from django.contrib.auth.models import User
# Create your models here.
from django.db import models
from django.utils import timezone

class Role_nav(models.Model):
    id = models.CharField(verbose_name="编号",max_length=20, primary_key=True, )
    title = models.CharField(max_length=20,verbose_name="角色")
    sitename= models.CharField(max_length=20,verbose_name="站点名称")
    siteIcon=models.ImageField(verbose_name="图标",upload_to = 'img', default="/img/default.ico")
    createUser = models.ForeignKey(User, on_delete=models.PROTECT,verbose_name="创建者")
    createTime = models.DateTimeField(default=timezone.now,verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="修改时间")
    def __str__(self):
        return self.title
    class Meta:
        verbose_name = '角色导航'
        verbose_name_plural = verbose_name

class Nav_bar(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True, )
    title = models.CharField(max_length=20,verbose_name="标题")
    url = models.CharField(max_length=225, verbose_name="链接",blank=True ,null=True,default="")
    icon = models.ImageField(verbose_name="图标",blank=True ,null=True,default="/img/default.ico")
    role_nav = models.ForeignKey('Role_nav', on_delete=models.PROTECT, verbose_name="角色导航")
    sort = models.IntegerField(verbose_name="排序", default=1)
    createTime = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    def __str__(self):
        return self.title
    class Meta:
        verbose_name = '导航条'
        verbose_name_plural = verbose_name

class Left_bar(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True, )
    nav_bar = models.ForeignKey('Nav_bar', on_delete=models.PROTECT, verbose_name="导航条")
    title = models.CharField(max_length=20,verbose_name="标题")
    icon = models.ImageField(verbose_name="图标",blank=True ,null=True,default="/img/default.ico")
    sort = models.IntegerField(verbose_name="排序", default=1)
    createTime = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    def __str__(self):
        return self.title
    class Meta:
        verbose_name = '左导航条'
        verbose_name_plural = verbose_name

class Links(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True, )
    left_bar = models.ForeignKey('Left_bar', on_delete=models.PROTECT, verbose_name="左导航条")
    title = models.CharField(max_length=20,verbose_name="标题")
    icon = models.ImageField(verbose_name="图标",blank=True ,null=True,default="/img/default.ico")
    url = models.CharField(max_length=225,verbose_name="链接" )
    des =models.TextField(verbose_name="说明",blank=True ,null=True)
    sort = models.IntegerField(verbose_name="排序", default=1)
    createTime = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="修改时间")
    def show_nav_bar_title(self):
        o= self.left_bar.nav_bar
        if o is not None:
            return o.title
        return ""

    show_nav_bar_title.short_description = u'导航条'  # 指定后台显示列表字段名

    def __str__(self):
        return self.title
    class Meta:
        verbose_name = '链接'
        verbose_name_plural = verbose_name

class Solink(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True, )
    role_nav = models.ForeignKey('Role_nav', on_delete=models.PROTECT, verbose_name="角色导航")
    title = models.CharField(max_length=20,verbose_name="标题")
    icon = models.ImageField(verbose_name="图标",blank=True ,null=True,default="/img/default.ico")
    url = models.CharField(max_length=225,verbose_name="链接" )
    des =models.TextField(verbose_name="说明",blank=True ,null=True)
    sort = models.IntegerField(verbose_name="排序", default=1)
    createTime = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    def __str__(self):
        return self.title
    class Meta:
        verbose_name = '搜索链接'
        verbose_name_plural = verbose_name

